/*
 * Copyright (c) 2017. 深圳一七科技. All rights reserved.
 */

package com.yiqi.liebang.feature.mine.presenter;

import android.text.TextUtils;

import com.suozhang.framework.component.http.ApiError;
import com.suozhang.framework.component.http.ex.ApiException;
import com.suozhang.framework.entity.bo.UserInfoBo;
import com.suozhang.framework.utils.ValidatorUtil;
import com.yiqi.liebang.common.util.RxRegTool;
import com.yiqi.liebang.entity.LikeUserBo;
import com.yiqi.liebang.entity.bo.RelationshipBo;
import com.yiqi.liebang.entity.bo.UserCardBo;
import com.yiqi.liebang.entity.bo.UserCenterBo;
import com.yiqi.liebang.entity.bo.UserDataBo;
import com.yiqi.liebang.feature.mine.contract.UserContract;
import com.yiqi.liebang.feature.mine.dependencies.login.DaggerUserPresenterComponent;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * LIJUWEN
 *
 * @email yiyayiyayaoljw@gmail.com
 * @date 2018/7/11 9:31
 */
public class UserPresenter implements UserContract.Presenter {

    private UserContract.View mView;

    @Inject

    UserContract.Model mModel;


    public UserPresenter(UserContract.View mView) {
        this.mView = mView;
        DaggerUserPresenterComponent.create().inject(this);
    }

    @Override
    public void start() {

    }

    @Override
    public void getUserData(String userUid, int type, String visitorUid) {
        if (userUid == null) return;
        mModel.getUserData(new UserInfoBo(userUid, type, visitorUid)).compose(mView.bindToLife()).subscribe(new Observer<UserCenterBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(UserCenterBo userCenterBo) {
                mView.showUserCenterSuccess(userCenterBo);
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                ApiException exception = (ApiException) e;
                ApiError error = ApiError.codeOf(exception.code());
                if (error == ApiError.NOT_AUTHORIZED_ACCESS) {
                    mView.showUserCenterError(e.getMessage());
                } else {
                    mView.showError(e.getMessage());
                }
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }


    @Override
    public void getUserCardData() {
        mModel.getUserCardData().compose(mView.bindToLife()).subscribe(new Observer<UserCardBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(UserCardBo userCenterBo) {
                mView.showUserCardSuccess(userCenterBo);
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.showError(e.getMessage());
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void editUserCardData(String userHead, String userName,
                                 String userLabelId, String userIndustry, String userWorkAddress,
                                 String userDetailAddress, String userPhone, String userEmail) {

        if (TextUtils.isEmpty(userName)) {
            mView.showError("真实姓名不能为空！");
            return;
        }

        if (!TextUtils.isEmpty(userPhone)) {
            if (!ValidatorUtil.isMobile(userPhone)) {
                mView.showError("手机号格式不正确！");
                return;
            }
        }

        if (!TextUtils.isEmpty(userEmail)) {
            if (!RxRegTool.isEmail(userEmail)) {
                mView.showError("请输入正确的邮箱！！");
                return;
            }
        }


        mModel.editUserCardData(userHead, userName, userLabelId, userIndustry, userWorkAddress, userDetailAddress, userPhone, userEmail).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String s) {
                mView.showUserEditSuccess();
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.showError(e.getMessage());
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void likeUser(String likeUserUid, int type) {

        mModel.likeUser(new LikeUserBo(likeUserUid, type)).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String userCenterBo) {
                mView.showLikeSuccess();
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.showError(e.getMessage());
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void privateUser(String userUid) {
        mModel.privateUser(new UserInfoBo(userUid)).compose(mView.bindToLife()).subscribe(new Observer<Boolean>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(Boolean userCenterBo) {
                mView.showPrivateSuccess(userCenterBo);
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.showError(e.getMessage());
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void getMineData() {
        mModel.getMineData().compose(mView.bindToLife()).subscribe(new Observer<UserDataBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(UserDataBo userCenterBo) {
                mView.showMineSuccess(userCenterBo);
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.showError(e.getMessage());
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void deleteFriend(String deleteUserId) {
        RelationshipBo relationshipBo = new RelationshipBo();
        relationshipBo.setFriendUid(deleteUserId);
        mModel.deleteFriend(relationshipBo).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String s) {
                mView.dismissLoading();
                mView.showDeleteFriendSuccess();
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void addBlack(String blackUserId) {
        RelationshipBo relationshipBo = new RelationshipBo();
        relationshipBo.setBlackUserUid(blackUserId);
        mModel.addBlack(relationshipBo).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String s) {
                mView.dismissLoading();
                mView.showAddBlackSuccess();
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

}
